local skel = fk.CreateSkill{
  name = "tea__souliu",
  tags = { Skill.Compulsory },
}
local U = require "packages/utility/utility"
local TeaU = require "packages.wild_collection.utility.utility"
Fk:loadTranslationTable{
  ["tea__souliu"] = "飕飗",
  [":tea__souliu"] = "锁定技，当“风”转动后，若X为0，防止你本回合受到的属性伤害和锦囊牌伤害；若X不为0，你用牌堆顶X张牌交换等量的“风”，"..
  "然后令一名角色摸至多X张牌（X为“风”牌花色对应正确的数量）。",
  ["tea__souliu-drawcard"] = "飕飗：你可以令一名角色摸至多 %arg 张牌！",
  ["@@tea__souliu-turn"] = "飕飗",
}
local getCorrectNum = function (player)
  local ids = player:getMark("tea__sixiang_record")
  local c_suits = { Card.Diamond, Card.Heart, Card.Club, Card.Spade }
  local num = 0
  for k, id in ipairs(ids) do
    if Fk:getCardById(id).suit == c_suits[k] then
      num = num + 1
    end
  end
  return num
end
---获得语音数字
---@param player any
---@return integer
local getVoice = function (player)
  local isMale = player.phase ~= Player.NotActive
  if isMale then
    return table.random{1, 2}
  else
    return table.random{3, 4}
  end
end
---@type TrigSkelSpec<TrigFunc>
local souliu = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and #player:getMark("tea__sixiang_record") == 4
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "special")
    player:broadcastSkillInvoke(skel.name, getVoice(player))
    local num = getCorrectNum(player)
    if num == 0 then
      room:setPlayerMark(player, "@@tea__souliu-turn", 1)
    else
      local cards = room:getNCards(num)
      local mark = player:getTableMark("tea__sixiang_record")
      local data_table = {
        cards = cards,
        ori_cards = mark,
        ok_option = {"OK"},
        ban_swap = true,
      }
      local result = player.room:askToCustomDialog(player, {
        skill_name = skel.name,
        qml_path = "packages/wild_collection/qml/SixiangBox.qml",
        extra_data = data_table,
        }
      )
      local cids
      if result ~= "" then
        cids = json.decode(result).cardMap
        local toSpecial, toDraw = {}, {}
        for i = 1, 10 do
          if cids[i] then
            if i < 5 then
              table.insertIfNeed(toSpecial, cids[i])
            else
              table.insertIfNeed(toDraw, cids[i])
            end
          else
            break
          end
        end
        if not player.dead and #toSpecial > 0 then
          local _mark = {}
          _mark._tab = toSpecial
          room:setPlayerMark(player, "@[sixiang]", _mark)
          room:setPlayerMark(player, "tea__sixiang_record", toSpecial)
          player:addToPile("#tea__sixiang_pile", toSpecial, true, skel.name, player)
        else
          room:moveCardTo(toSpecial, Card.DiscardPile, nil, fk.ReasonDiscard)
        end
        if not player.dead and #toDraw > 0 then
          room:moveCardTo(toDraw, Card.DrawPile, nil, fk.ReasonDiscard, skel.name, nil, true, player)
        end
      end
      if not player.dead then
        local mod_num = getCorrectNum(player)
        if mod_num > 0 then
          local suc, dat = room:askToUseActiveSkill(player, {
            skill_name = "tea__souliu_active",
            prompt = "tea__souliu-drawcard:::" .. tostring(mod_num),
            extra_data = {
              num = mod_num,
            },
          })
          if suc and dat then
            local to = dat.targets[1]
            local choose_num = dat.interaction
            to:drawCards(choose_num, skel.name)
          end
        end
      end
    end
  end
}
---@type TrigSkelSpec<DamageTrigFunc>
local souliu_delay = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player:getMark("@@tea__souliu-turn") > 0 then
      if data.damageType ~= fk.NormalDamage then
        return true
      elseif data.card and data.card.type == Card.TypeTrick then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:notifySkillInvoked(player, skel.name, "defensive")
    data:preventDamage()
    return true
  end
}
skel:addEffect(TeaU.AfterSixiangChanged, souliu)
skel:addEffect(fk.DamageInflicted, souliu_delay)
return skel